package com.example.powernodemall.strategy.Impl;

import com.example.powernodemall.constant.BaseConstant;
import com.example.powernodemall.mapper.SysUserMapper;
import com.example.powernodemall.model.LoginSysUser;
import com.example.powernodemall.model.SecurityUser;
import com.example.powernodemall.strategy.LoginMethod;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component(value = BaseConstant.SysUser)
public class FinalSysLogin implements LoginMethod {
    @Resource
    SysUserMapper sysUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) {
        LoginSysUser loginSysUser = sysUserMapper.selectByUsername(username);
        if (loginSysUser == null) {
            throw new UsernameNotFoundException("当前用户不存在");
        }
        Set<String> permissions = sysUserMapper.selectPermissionsById(loginSysUser.getUserId());
        SecurityUser securityUser=new SecurityUser();
        securityUser.setUserId(loginSysUser.getUserId());
        securityUser.setPassword(loginSysUser.getPassword());
        securityUser.setUsername(loginSysUser.getUsername());
        securityUser.setLogintype(BaseConstant.SysUser);
        securityUser.setStatus(loginSysUser.getStatus());
        securityUser.setRoles(permissions);
        securityUser.setShopId(loginSysUser.getShopId());
        return securityUser;
    }
}
